#!/bin/bash
OUT=$(pwd)
# 确定源
if [ ! "${ELECTRON_MIRROR}" ]; then
    export ELECTRON_MIRROR="https://github.com/electron/electron/releases/download/v"
fi
# 确定版本
if [ ! "${ELECTRON_VERSION}" ]; then
    export ELECTRON_VERSION=$(basename -s .sh $0 | awk -F@ '{ print $2 }')
fi
if [ ! "${ELECTRON_VERSION}" ]; then
    export ELECTRON_VERSION=$(wget -O- https://unpkg.com/electron/package.json | jq -r .version)
fi
# 确定包名
ELECTRON_MAIN_VERSION=$(echo $ELECTRON_VERSION | awk -F. '{ print $1 }')
if [ ! "${PACKAGE_NAME}" ]; then
    export PACKAGE_NAME="org.electron.spark.v${ELECTRON_MAIN_VERSION}"
fi
# 确定架构
if [ ! "${ARCH}" ]; then
    export ARCH=$(dpkg --print-architecture)
fi
ELECTRON_ARCH=$(echo $ARCH | sed s/amd64/x64/g | sed s/aarch64/arm64/g)
# 生成下载地址
DLURL="${ELECTRON_MIRROR}${ELECTRON_VERSION}/electron-v${ELECTRON_VERSION}-linux-${ELECTRON_ARCH}.zip"
# 创建文件目录
mkdir -p "/tmp/buildERT/${PACKAGE_NAME}"
cd "/tmp/buildERT/${PACKAGE_NAME}"
# 下载并解压
mkdir -p opt/durapps/electron${ELECTRON_MAIN_VERSION}
wget $DLURL -O- | busybox unzip - -d "opt/durapps/electron${ELECTRON_MAIN_VERSION}"
# 权限修复
chmod 0755 "opt/durapps/electron${ELECTRON_MAIN_VERSION}/electron"
# 生成桌面快捷方式
mkdir -p usr/share/applications
echo "[Desktop Entry]
Name=electron${ELECTRON_MAIN_VERSION}
Comment=Build cross-platform desktop apps with JavaScript, HTML, and CSS
GenericName=electron
Exec=/opt/durapps/electron${ELECTRON_MAIN_VERSION}/electron %U
Icon=electron
Type=Application
NoDisplay=true
StartupNotify=true
Categories=Utility;Development;
MimeType=application/asar;
" >"usr/share/applications/${PACKAGE_NAME}.desktop"
# 生成control
mkdir -p DEBIAN
echo "Package: ${PACKAGE_NAME}
Version: ${ELECTRON_VERSION}
Architecture: ${ARCH}
Maintainer: dzio <2592455724@qq.com>
Installed-Size: $(du -s /tmp/buildERT/$PACKAGE_NAME/ | awk '{ print $1 }')
Section: utils
Priority: optional
Homepage: https://www.electronjs.org/
Description: Build cross-platform desktop apps with JavaScript, HTML, and CSS" >"DEBIAN/control"
# 打包
dpkg-deb -b . $OUT
